package reedSolomon;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import fileDistributionStatistics.FileSizeDistributionCalculation;
import fileDistributionStatistics.FileSizeInfo;

public class FragSizeCalculation {
	File fFile;
	File filename = new File(
			"/home/bhavya/Desktop/BHAVYA_FYP/Cloud_Computing_FYP/FileFragmentSizeCalculation_2/reedsolomon.txt");
	int startingFileSize = 1;
	int sumOfSmallFileSizes = 0;
	List<FileSizeInfo> fileInfo = new ArrayList<FileSizeInfo>();
	FileWriter fstream;
	BufferedWriter out;

	public FragSizeCalculation(File aFileName) {
		fFile = aFileName;
	}

	public final void processLineByLine() throws FileNotFoundException {
		// Note that FileReader is used, not File, since File is not Closeable
		Scanner scanner = new Scanner(new FileReader(fFile));
		try {

			// first use a Scanner to get each line
			while (scanner.hasNextLine()) {
				processLine(scanner.nextLine());
			}
		} finally {
			scanner.close();
		}
	}

	protected void processLine(String aLine) {
		// use a second Scanner to parse the content of each line
		Scanner scanner = new Scanner(aLine);

		int rep1;
		scanner.useDelimiter(":");
		if (scanner.hasNext()) {
			String fs = scanner.next().trim();
			String rep = scanner.next().trim();
			rep1 = Integer.parseInt(rep);
			// rep1=1;
			// System.out.println(" " + fs + " " + rep1);
			fileInfo.add(new FileSizeInfo(Double.parseDouble(fs), Integer
					.parseInt(rep)));
		}
		scanner.close();
	}
/****
 * preprocessFile code removed if needed copy from ParseFile
 */
	
	public void calculateFragmentSize() {
		// Change the code to read values from the arrayList !!
		// TODO read values from ArrayList Instead of directly reading
		//FileSizeDistributionCalculation f = new FileSizeDistributionCalculation();
		try {
			fstream = new FileWriter(filename);
			out = new BufferedWriter(fstream);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		double fragSize=0;
		double cumSpace = 0;
		RSRatioOfFragments rs = new RSRatioOfFragments();
		for (int i = 0; i < fileInfo.size(); i++) {
			cumSpace=0;
			double spaceOccupiedOnCloud = 0;
			double source_file = fileInfo.get(i).getFileSize();
			int rep = fileInfo.get(i).getNumberOfFiles();
			try {
				out.write(source_file + ",");
//				source_file = source_file * rep;
//				out.write(rep+",");
//				for( int j=0;j<rep;j++){
					cumSpace=0;
				if (source_file <= 4096) {
					fragSize=4096;
					spaceOccupiedOnCloud = fragSize * 8;
				} else {
//					source_file=source_file*rep;
					/***
					 * file size greater than 4096 but lesser than 64 MB
					 */
					if (source_file > 4096 && source_file <= 64 * 1024 * 1024) {
						/***
						 * file size lesser than 256 KB
						 */
						if (source_file <= 256 * 1024) {
							fragSize=4096;
							int numOfSrcFrags=(int)Math.ceil((double)source_file/fragSize);
							spaceOccupiedOnCloud = fragSize
									* (RSRatioOfFragments.ratioMapping.get(numOfSrcFrags)+numOfSrcFrags);
						} else {
							/**
							 * fileSize greater than 256KB
							 */
							int factor= (int) (source_file % (256*1024));
							if(factor!=0){
								source_file=source_file+((256*1024)-factor);
							}
							
							fragSize=Math.ceil((double)source_file/64);
							spaceOccupiedOnCloud=96*fragSize;
						}
					} else {
						/***
						 * file size greater than 64MB.
						 */
//						System.out.println(source_file);
//						if(source_file==67108904){
//							System.out.println("help");
//						}
						int n1 = (int) source_file / (64 * 1024 * 1024);
						spaceOccupiedOnCloud = (double)n1 * 96 * 1024 * 1024;
						double n2=source_file-(n1*64*1024*1024);
						if (n2 != 0) {
							
							double interSpace = 0;
							if (n2 < 256 * 1024) {

								 if(n2 < 4096){
									 fragSize=4096;
									interSpace=8*fragSize; 
								 } else{
									 double fragsize=4096;
								int numOfSrcFrags = (int)((double) n2 /fragsize );
								
								 interSpace = fragsize
										* (rs.ratioMapping.get(numOfSrcFrags)+numOfSrcFrags);
								 }

							} else {
								int factor=(int) Math.ceil(n2%256*1024);
								if(factor!=0){
									n2=n2+(256*1024-factor);
								}
								fragSize = Math.ceil((double) n2 / 64);
								 interSpace = 96 * (fragSize);
							}
							spaceOccupiedOnCloud = spaceOccupiedOnCloud
									+ interSpace;
						}
						
					}//file greater than 64MB
				}// end of else file sizes greater than 4KB
//				System.out.println(source_file+":"+spaceOccupiedOnCloud);
				cumSpace=spaceOccupiedOnCloud*rep;
//			}// end of for-loop rep
//				cumSpace += spaceOccupiedOnCloud;
				out.write(String.valueOf(cumSpace)+",");
				out.write(String.valueOf(rep)+",");
				out.write(String.valueOf(fragSize)+",");
				int f=(int) (source_file%4096);
				if(f!=0){
					source_file=source_file+(4096-f);
				}
				double r=spaceOccupiedOnCloud/source_file;
				out.write(String.valueOf(r));
				out.newLine();
			} catch (Exception e) {

			}

		}// end of for loop for source file
		try {
			out.close();
		} catch (Exception e) {

		}
	}

	public static void main(String[] args) {
		File parseFilename = new File(
				"/home/bhavya/Desktop/BHAVYA_FYP/Cloud_Computing_FYP/FileSizeDistributionStatistics/CalculatedfileSizeGreatertan1byte.txt");

		FragSizeCalculation parser = new FragSizeCalculation(parseFilename);
		try {
			parser.processLineByLine();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// parser.preProcessFileSize();
		parser.calculateFragmentSize();
		// System.out.println("done");
	}


}
